import scrapy

from dangdang.items import DangdangItem


# 任务爬取当当网java书籍100页的书籍名称和图片

class BookSpider(scrapy.Spider):
    name = "book"
    allowed_domains = ["category.dangdang.com"]
    start_urls = ["https://category.dangdang.com/cp01.54.06.06.00.00.html"]
    page=1
    def parse(self, response):
        #所有的seletor对象都可以调用xpath
       base_url = response.xpath('//div[@id="search_nature_rg"]/ul/li/a/img')
        #循环遍历获取每一个书籍的name和img
       for url in base_url:
           book_name= url.xpath('.//@alt').extract_first()
           book_img=url.xpath('.//@data-original').extract_first()
           # 图片懒加载进行判断
           if book_img:
               book_img=book_img
           else:
               book_img = url.xpath('.//@src').extract_first()
            # 导入items定义好的爬虫数据并将爬取的数据交给items
           books= DangdangItem(book_name=book_name, book_img=book_img)
           #调用yield将爬取的数据交给管道（一个一个交）
           yield  books
       for i in range(1,10):
            base_url='https://category.dangdang.com/pg'+str(i)+'-cp01.54.06.06.00.00.html'
            yield scrapy.Request(url=base_url,callback=self.parse)